home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / ExString.h < prev    next >
C/C++ Source or Header  |  1990-05-15  |  3KB  |  86 lines

  1. // ExString.h -- Dynamic character strings
  2.  
  3. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ExString.h,v 3.0 90/05/15 22:43:35 kgorlen Rel $
  4.  
  5. #include <string.h>
  6. #include <iostream.h>
  7.  
  8. const unsigned EXTRA = 7;   // default # characters extra
  9. typedef int bool;
  10. class String;
  11.  
  12. class SubString {
  13.     char* sp;               // substring pointer 
  14.     unsigned sl;            // substring length 
  15.     SubString(const String&, unsigned, unsigned);
  16.     SubString(const SubString&);        // private copy constructor
  17.     friend String;
  18. public:
  19.     void operator=(const SubString& from);
  20.     bool operator==(const String&) const;
  21.     String operator&(const String&) const;
  22. };
  23.  
  24. class String  {
  25.     char* p;            // pointer to character string
  26.     unsigned len;       // length of string
  27.     unsigned alloc;     // amount of storage allocated
  28.     friend SubString;
  29. public:
  30.     String(unsigned extra=EXTRA);   // construct an empty String
  31.     String(const char* cs, unsigned extra=EXTRA);
  32.                                     // construct from C string
  33.     String(const String&);          // construct one String
  34.                                     // from another
  35.     String(const SubString&);       // construct from a SubString
  36.     ~String() { delete(p); }
  37.     operator const char*() const { return p; }
  38.                                     // convert to a C string
  39.     SubString operator()(unsigned pos, unsigned lgt);
  40.     const SubString operator()(unsigned pos, unsigned lgt) const;
  41.     char& operator[](unsigned pos) {
  42. //      if (pos >= len)  error
  43.         return p[pos];
  44.         }
  45.     char operator[](unsigned pos) const {
  46. //      if (pos >= len)  error
  47.         return p[pos];
  48.         }
  49.     void operator=(const String&);
  50.     bool operator==(const String& s) const {
  51.         return strcmp(p, s.p) == 0;
  52.     }
  53.     String operator&(const String& s) const;
  54.     void operator&=(const String& s);
  55.     unsigned length() const   { return len; }
  56.     unsigned capacity() const { return alloc-1; }
  57.     void printOn(ostream& strm) const;
  58.     void scanFrom(istream& strm);
  59. };
  60.  
  61. inline SubString::SubString(const String& s,
  62.                             unsigned pos, unsigned lgt)
  63. {
  64.     sp = &((String&)s)[pos];
  65. //  if (pos+lgt >= s.len)  error
  66.     sl = lgt;
  67. }
  68.  
  69. inline SubString::SubString(const SubString& ss)
  70. {
  71.     sp = ss.sp;  sl = ss.sl;
  72. }
  73.  
  74. inline void SubString::operator=(const SubString& from)
  75. {
  76.     strncpy(sp, from.sp, sl);
  77. }
  78.  
  79. inline bool SubString::operator==(const String& s) const
  80. {
  81.     return strncmp(sp, s.p, sl) == 0;
  82. }
  83.  
  84. extern ostream& operator<<(ostream&, const String&);
  85. extern istream& operator>>(istream&, String&);
  86.